package com.cole.gx095.controller;


import com.cole.gx095.Repository.PurchasebillRepository;
import com.cole.gx095.Repository.StockgoodsRepository;
import com.cole.gx095.domain.StockVo;
import com.cole.gx095.domain.mapper.StockMapper;
import com.cole.gx095.service.StockService;
import com.cole.gx095.controller.utils.CommonRep;
import com.cole.gx095.entity.Purchasebill;
import com.cole.gx095.entity.Stockgoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/stock")
@CrossOrigin
@Transactional
public class stockController {
    @Autowired
    StockgoodsRepository stockgoodsRepository;
    @Autowired
    PurchasebillRepository purchasebillRepository;
    @Autowired
    StockService stockService;
    @GetMapping("/List")
    CommonRep List(Integer currentPage,Integer pageSize){
        Page<Stockgoods> all = stockgoodsRepository.findAll(PageRequest.of(currentPage - 1, pageSize));
        List<Stockgoods> content = all.getContent();
        List<StockVo> stockVos = new ArrayList<>();
        for (Stockgoods stockgoods : content) {
            Purchasebill byId = purchasebillRepository.getById(stockgoods.getBillId());
            StockVo conver = StockMapper.INSTANCT.conver(stockgoods);
            conver.setBillDate(byId.getBillDate());
            conver.setBillType(byId.getBillType());
            stockVos.add(conver);
        }
        stockVos.forEach(System.out::println);
        return CommonRep.ok()
                .data("List",stockVos)
                .data("page",all);
    }
    @PostMapping("/query")
    CommonRep query(Integer currentPage,
                    Integer pageSize,
                    Stockgoods stockgoods){
        Page<Stockgoods> allBy = stockService.findAllBy(stockgoods, currentPage, pageSize);
        List<Stockgoods> content = allBy.getContent();
        List<StockVo> stockVos = new ArrayList<>();
        for (Stockgoods stockgoods1 : content) {
            Purchasebill byId = purchasebillRepository.getById(stockgoods1.getBillId());
            StockVo conver = StockMapper.INSTANCT.conver(stockgoods1);
            conver.setBillDate(byId.getBillDate());
            conver.setBillType(byId.getBillType());
            stockVos.add(conver);
        }
        stockVos.forEach(System.out::println);
        return CommonRep.ok()
                .data("page",allBy)
                .data("List",stockVos);
    }
    @PostMapping("/saveRemarks")
    CommonRep saveRemarks(@RequestBody StockVo stockVo){
        System.out.println("stockVo = " + stockVo);
        Stockgoods stockgoods = stockgoodsRepository.getById(stockVo.getId());
        stockgoods.setRemakes(stockVo.getRemakes());
        stockgoodsRepository.save(stockgoods);
        return CommonRep.ok();
    }

}
